
/**
 * 会员
 * @param {nickName: string} 明文nick
 * @param {session: string} 
 */
exports.getMember = async (ctx) => {
  const { data: { nickName = "", session = "" }, cloud } = ctx
  try {
    const result = await cloud.topApi.invoke({
      api: 'taobao.crm.member.identity.get',
      data: {
        'nick': nickName,
        'session': session //context.accessToken
      }
    });
    return {
      success: true,
      data: result.result.member_info
    };
  } catch (err) {
    logger.info(ctx, `logger.info===会员查询:${JSON.stringify(err)}`);
    return {
      success: false,
      data: err
    }
  }
};

/**
 * 入会链接
 * @param {url: string} 回调链接
 * @param {session: string} 
 */
exports.getMemberUrl = async (ctx) => {
  const { data: { url = "", session = "" }, cloud } = ctx
  let date = new Date().getTime()
  try {
    const result = await cloud.topApi.invoke({
      api: 'taobao.crm.member.joinurl.get',
      data: {
        'callback_url': url + "?t=" + date,
        'extra_info': '{\"source\":\"isvapp\",\"activityId\":\"12345677\",\"entrance\":\"hudong\"}',
        'session': session //context.accessToken
      }
    });
    return {
      success: true,
      data: result
    };
  } catch (err) {
    logger.info(ctx, `logger.info===入会链接获取:${JSON.stringify(err)}`);
    return {
      success: false,
      data: err
    }
  }
};

/**
 * 积分
 * @param {nickName: string} 明文nick
 * @param {session: string} 
 */
exports.getiIntegral = async (ctx) => {
  const { data: { nickName = "", session = "" }, cloud } = ctx
  try {
    const result = await cloud.topApi.invoke({
      api: 'taobao.crm.point.available.get',
      data: {
        'buyer_nick': nickName,
        'session': session  //context.accessToken
      }
    });
    return {
      success: true,
      data: result
    };
  } catch (err) {
    logger.info(ctx, `logger.info===积分${JSON.stringify(err)}`);
    return {
      success: false,
      data: err
    };
  };
};

/**
 * 积分变更
 * @param {num: number} 积分
 * @param {nickName: string} 明文nick
 * @param {mixNick: string} 密文nick
 * @param {opt_type: string} 操作类型，0：增加，1：扣减
 * @param {session: string} 
 */
exports.changeIntegral = async (ctx) => {
  const { data: { opt_type="0", num = 5, nickName = "", mixNick = "", session = "" }, cloud } = ctx
  try {
    const result = await cloud.topApi.invoke({
      api: 'taobao.crm.point.change',
      data: {
        'change_type': "1",
        'opt_type': opt_type,
        'quantity': num,
        'nickName': nickName,
        'mix_nick': mixNick,
        'session': session //context.accessToken
      }
    });
    return {
      success: true,
      data: result
    }
  } catch (err) {
    logger.info(ctx, `logger.info===积分修改${JSON.stringify(err)}`);
    return {
      success: false,
      data: err
    }
  }
};


/**
 * 商品类目查询
 * @param {parent_cid: string} 父商品类目 id
 * @param {session: string} 
 */
exports.getCategoryList = async (ctx) => {
  const { data: { cids = "", parent_cid = 0, session = '' }, cloud } = ctx
  try {
    const result = await cloud.topApi.invoke({
      api: 'taobao.itemcats.get',
      data: {
        'fields': 'cid,parent_cid,name,is_parent',
        'parent_cid': parent_cid,
        'session': session  //context.accessToken
      }
    });
    return {
      success: true,
      data: result.item_cats
    }
  } catch (err) {
    logger.info(ctx, `logger.info===商品查询${JSON.stringify(err)}`);
    return {
      success: false,
      data: err
    }
  }
};

/**
 * 商品列表查询
 * @param {page_size: string} 每页条数
 * @param {index: string} 当前页码
 * @param {session: string} 
 */
exports.getGoodsList = async (ctx) => {
  const { data: { page_size = 10, index = 1, session = '' }, cloud } = ctx
  try {
    const result = await cloud.topApi.invoke({
      api: 'taobao.items.onsale.get',
      data: {
        'fields': 'num_iid,title,cid,pic_url,num,price,sold_quantity',
        'page_size': page_size,
        'page_no': index,
        'session': session  //context.accessToken
      }
    });
    return {
      success: true,
      data: result.items
    }
  } catch (err) {
    logger.info(ctx, `logger.info===商品查询${JSON.stringify(err)}`);
    return {
      success: false,
      data: err
    }
  }
};


/**
 * 商品详情查询
 * @param {num_iids: []} 列表
 */
exports.getGoodsItem = async (ctx) => {
  const { data: { num_iids = [], session = '' }, cloud } = ctx
  try {
    const result = await cloud.topApi.invoke({
      api: 'taobao.items.seller.list.get',
      data: {
        'fields': 'num_iid,title,nick,approve_status,num,sku',
        'num_iids': num_iids.toString(),
        'session': session 
      }
    });
    return {
      success: true,
      data: result.items
    };
  } catch (err) {
    logger.info(ctx, `logger.info===商品查询${JSON.stringify(err)}`);
    return {
      success: false,
      data: err
    }
  }
};


/**
 * 获取商家Session
 * @param {code: string} //code
 */
exports.getSession = async (ctx) => {
  const { data: { code = "" }, cloud } = ctx
  try {
    const result = await cloud.topApi.invoke({
      api: 'taobao.openlink.session.get',
      data: { "code": code }
    });
    return {
      success: true,
      data: result
    };
  } catch (err) {
    logger.info(ctx, `logger.info===获取商家Session${JSON.stringify(err)}`);
    return {
      success: false,
      data: err
    }
  }
};
